// let _UrlGet_ = wx.getStorageSync('wx_url')
let _UrlGet_ = wx.getStorageSync('wx_url')
let _baseUrlGet_ = wx.getStorageSync('img_url')

module.exports = {
  requestUrl: function (data, url, callback, pageobj) {
    wx.request({
      url: url,
      data: data,
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Authorization': getApp().globalData.token
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.code > 0) {
          callback.apply(pageobj, [res.data])
        } else {
          var error_msg = 'error:接口请求错误';
          if (res.data.info != 'null' || res.data.info != '') {
            error_msg = res.data.info
          }
          wx.showModal({
            title: '提示',
            content: error_msg,
            showCancel: false
          })
        }
      },
      complete: function () { }
    })
  },
  httpRequest: function (data, url, callback, pageobj) {
    wx.request({
      url: url,
      data: data,
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Authorization': getApp().globalData.token
      },
      success: function (res) {
        if (res.statusCode != 200) {
          wx.showModal({
            title: '提示',
            content: "error:接口请求错误",
            showCancel: false
          })
        } else {
          callback.apply(pageobj, [res.data]);
          if (res.data.code != 2 && res.data.code != 1) {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false
            })
          }
        }
      },
      fail: function () {
        wx.showModal({
          title: '提示',
          content: "error:网络请求失败",
          showCancel: false
        })
      }
    })
  },
  // 全部返回 post
  post: function (data, url, callback, pageobj) {
    wx.request({
      url: url,
      data: data,
      method: 'post',
      header: {
        'Authorization': getApp().globalData.token
      },

      success: callback,
      fail: function (res) {
        wx.showModal({
          title: '错误提示',
          content: res.ms,
          showCancel: false
        })
      },
      complete: function (res) {
      }
    })
  },
  // 全部返回 get
  get: function (data, url, callback, pageobj) {
    // 拼接查询参数
    let params = "";
    for (let key in data) {
      if (data[key] != null && data[key] != "") {
        if (params.length === 0) {
          params += `?${key}=${encodeURIComponent(data[key])}`; // 使用 encodeURIComponent 避免特殊字符问题
        } else {
          params += `&${key}=${encodeURIComponent(data[key])}`;
        }
      }
    }

    // 确保 URL 拼接正确，移除多余的空格
    url = url.trim() + params;

    wx.request({
      url: url,
      method: "GET",
      header: {
        "Authorization": getApp().globalData.token
      },
      success: function (res) {
        if (callback && typeof callback === "function") {
          callback(res, pageobj);
        }
      },
      fail: function (res) {
        wx.showModal({
          title: "错误提示",
          content: res.errMsg || "请求失败，请检查网络或重试",
          showCancel: false
        });
      }
    });
  },
  // PUT 请求
  put: function (data, url, callback, pageobj) {
    wx.request({
      url: url,
      data: data,
      method: 'PUT',
      header: {
        'Authorization': getApp().globalData.token,
        'Content-Type': 'application/json' // 根据后端要求设置
      },
      success: callback,
      fail: function (res) {
        wx.showModal({
          title: '错误提示',
          content: res.errMsg || '网络请求失败',
          showCancel: false
        });
      },
      complete: function (res) {
        console.log('PUT 请求完成:', res);
      }
    });
  },
  // DELETE 请求
  delete: function (data, url, callback, pageobj) {
    wx.request({
      url: url,
      data: data,
      method: 'DELETE',
      header: {
        'Authorization': getApp().globalData.token,
        'Content-Type': 'application/json' // 根据后端要求设置
      },
      success: callback,
      fail: function (res) {
        wx.showModal({
          title: '错误提示',
          content: res.errMsg || '网络请求失败',
          showCancel: false
        });
      },
      complete: function (res) {
        console.log('DELETE 请求完成:', res);
      }
    });
  },

  // 注册登陆
  postwxappLogin: function (data, callback, pageobj) {
    let that = this
    let res = that.post(data, _UrlGet_ + '/user/login', callback, pageobj)
  },

  // 小程序-授权 头像 昵称 姓名 电话
  postLogin: function (data, callback, pageobj) {
    let that = this
    let res = that.post(data, _UrlGet_ + '/api/applet/login', callback, pageobj)
  },

  // 小程序-授权手机号
  postGetUserMobile: function (data, callback, pageobj) {
    let that = this
    let res = that.post(data, _UrlGet_ + '/api/applet/getUserMobile', callback, pageobj)
  },

  // 地区
  getTreeArea: function (data, callback, pageobj) {
    let that = this
    let res = that.get(data, _UrlGet_ + '/api/applet/treeArea', callback, pageobj)
  },

  // 获取信息
  getCarInfo: function (data, callback, pageobj) {
    let that = this
    let res = that.get(data, _UrlGet_ + '/car/getCarInfo ', callback, pageobj)
  },
  // 发送指令
  sendCarInfo: function (data, callback, pageobj) {
    let that = this
    let res = that.post(data, _UrlGet_ + '/car/sendCarInfo', callback, pageobj)
  },
  
}